package com.pro01.backend.controller;

import com.pro01.backend.pojo.PageResult;
import com.pro01.backend.pojo.Result;
import com.pro01.backend.pojo.ShopItem;
import com.pro01.backend.service.ShopItemService;
import com.pro01.backend.utils.TokenUtil;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin/shop")
public class AdminShopController {

    @Autowired
    private ShopItemService shopItemService;

    /**
     * 管理员添加商城商品
     */
    @PostMapping("/items")
    public Result<Void> addShopItem(@RequestBody ShopItem item,
                                    HttpServletRequest request) {

        // ✅ 权限校验（统一风格）
        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可以添加商品");
        }

        try {
            shopItemService.addItem(item);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加商品失败：" + e.getMessage());
        }
    }

    /**
     * 管理员下架商城商品
     */
    @PutMapping("/items/{itemId}/disable")
    public Result<Void> disableShopItem(@PathVariable Long itemId,
                                        HttpServletRequest request) {

        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可以下架商品");
        }

        try {
            shopItemService.disableItem(itemId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("下架商品失败：" + e.getMessage());
        }
    }

    /**
     * 管理员重新上架商城商品
     */
    @PutMapping("/items/{itemId}/enable")
    public Result<Void> enableShopItem(@PathVariable Long itemId,
                                       HttpServletRequest request) {

        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可以上架商品");
        }

        try {
            shopItemService.enableItem(itemId);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上架商品失败：" + e.getMessage());
        }
    }

    /**
     * 管理员查看所有商品
     */
    @GetMapping("/items")
    public Result<PageResult<ShopItem>> getAllShopItems(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {

        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可以查看商品列表");
        }

        try {
            PageResult<ShopItem> result = shopItemService.getAllItems(pageNum, pageSize);
            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败：" + e.getMessage());
        }
    }

    /**
     * 管理员审核晋升请求
     */
    @PutMapping("/orders/{orderId}/review")
    public Result<Void> reviewAdminApplication(@PathVariable Long orderId,
                                               @RequestBody Map<String, Object> body,
                                               HttpServletRequest request) {

        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可以审核申请");
        }

        try {
            boolean approve = (Boolean) body.get("approve");
            String reply = (String) body.getOrDefault("reply", null);

            shopItemService.reviewAdminApplication(orderId, approve, reply);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("审核失败：" + e.getMessage());
        }
    }


    @GetMapping("/orders")
    public Result<PageResult<Map<String, Object>>> getAllOrders(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            HttpServletRequest request) {

        String role = TokenUtil.getRole(request);
        if (!"ADMIN".equals(role)) {
            return Result.error("无权限，只有管理员可以查看兑换记录");
        }

        try {
            PageResult<Map<String, Object>> pageResult = shopItemService.getAllOrders(pageNum, pageSize);
            return Result.success(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败：" + e.getMessage());
        }
    }

}


